package sui.utils.zip;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
 * <p>
 * Description: zip包工具类
 * <p>
 * @author XQF SUI
 * @Created: 2016年7月8日 下午6:07:39
 * @version 1.0
 */
public class ZipUtils {
 

    /**
     * <p>
     * Description：将指定文件打成zip包
     * </p>
     * @author XQF SUI
     * @created 2016年7月8日 下午6:08:13
     * @param zipSavePath
     * @param fileNames
     * @return
     */
	public static boolean crateZip(String zipSavePath,List<String> fileNames) {
        
	    if (fileNames == null || fileNames.size() < 1) return false;
            
        try {
            ZipOutputStream zipWrit = new ZipOutputStream(
                    new FileOutputStream(zipSavePath + System.currentTimeMillis() + ".zip"));
            
            for (String fileName : fileNames) {
                File file = new File(fileName);
                FileInputStream fileIn = new FileInputStream(file);
                zipWrit.putNextEntry(new ZipEntry(file.getName()));
                
                byte[] bytes = new byte[1204];
                
                while (fileIn.read(bytes) > 0) {
                    zipWrit.write(bytes);
                }
                
                zipWrit.closeEntry();
                fileIn.close();
                file.delete();
            }
            
            zipWrit.flush();
            zipWrit.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        return true;
	}
}